module.exports = function ($scope, $resource, $modal,$stateParams,$state){
    

    $scope.companynum = 0;

     //返回列表
	$scope.goback = function(info){
		$state.go('app.sysnotice', info);
    };

     //查询分销商列表
     $scope.officList = function (){
        $resource('/api/as/sc/office/getDistributorList', {}, {}).save({}, function(res){
            if(res.errcode === 0){
                $scope.info.officAllList = res.data;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
  
     //选择机构
     $scope.choosecompany = function() {
        var modalInstance = $modal.open({
            template: require('../views/choosecompany.html'),
            controller: 'choosecompany',
            size: 'lg',
            resolve: {
                item : function () {
                    return $scope.info;
                },
                flag : function () {
                    return $scope.info == 1;
                }
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function(selectedItem) {
            init();
            $scope.info = selectedItem;
            $scope.companynum = $scope.info.companycodes.length;
            console.log("return");
            console.log($scope.info);
            
            
        });
    }


    init();
    function init(){
        $scope.info = $stateParams;
        $scope.info.companycodes = [];
        var aaa= $scope.info.send_companys;
        for(var i in aaa){
            $scope.info.companycodes.push(aaa[i]);
        }

    }


  



    //保存并发送
    $scope.save = function (){
        $scope.info.company_codes = [];
        
        for(i in $scope.info.companycodes){
            var company_code = {
                'company_code': $scope.info.companycodes[i].code
            };
            $scope.info.company_codes.push(company_code);
        }
        if($scope.info.title == undefined){
            alert('请输入标题！');
            return;
        }
        if($scope.info.info == undefined){
            alert('请输入内容！');
            return;
        }
        if($scope.info.companyType == undefined){
            alert('请选择类型！');
            return;
        }
        if($scope.info.companycodes.length == 0){
            alert('请选择发送分销商！');
            return;
        }
        console.log($scope.info);
        var para = {
            'title': $scope.info.title,
            'info': $scope.info.info,
            'company_codes': $scope.info.company_codes,
            'companyType': $scope.info.companyType,
            'msgType' : 'enterprise'
        }
        $resource('/api/ac/sc/informaticaService/save', {}, {}).save(para, function(res){
            if(res.errcode === 0){
               alert('发送成功!')
                $scope.goback();
            }else{
                alert(res.errmsg);
            }
        });
    }

    //保存
    $scope.saveonly = function (){
        $scope.info.company_codes = [];
        
        for(i in $scope.info.companycodes){
            var company_code = {
                'company_code': $scope.info.companycodes[i].code
            };
            $scope.info.company_codes.push(company_code);
        }
        console.log($scope.info);
        if($scope.info.title == undefined){
            alert('请输入标题！');
            return;
        }
        if($scope.info.info == undefined){
            alert('请输入内容！');
            return;
        }
        if($scope.info.companyType == undefined){
            alert('请选择类型！');
            return;
        }
        if($scope.info.companycodes.length == 0){
            alert('请选择发送分销商！');
            return;
        }
        var para = {
            'title': $scope.info.title,
            'info': $scope.info.info,
            'company_codes': $scope.info.company_codes,
            'companyType': $scope.info.companyType,
            'msgType' : 'enterprise'
        }
        $resource('/api/ac/sc/informaticaService/insertInformatica', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                alert('保存成功!')
                $scope.goback();
            }else{
                alert(res.errmsg);
            }
        });
    }

};